<?php
/*--------------------------
	file:	midp.class
	intro:	核心类功能基类
--------------------------*/
class Mid {
	private static $conf;
	private static $db;
	private static $autoload;
	private static $route;
	private static $app;
	private static $smarty;
	private static $cache;
	private static $helper;
	private static $module;
	private static $pool_obj;
	private static $pool_dbconfig = array ();
	private static $poll_obj = array();
	
	
	/**
	 * static db object sotage
	 *
	 * @return db object
	 */
	public static function db($key = false) {
		if (! $key)
			$key = 'default';
		if (empty ( self::$pool_dbconfig )) {
			self::$pool_dbconfig = self::config ( 'db' );
		}
		$regkey = 'db.' . $key; 
		if (! self::isRegistered($regkey)) {
			$config = self::$pool_dbconfig [$key];
			$engine = $config ['engine'];
			if (self::loadClass ( 'db/pdo' ) && class_exists ( 'Db_Pdo' )) {
				$instance = new Db_Pdo( $config );
				if ($instance->conn) {
					//self::$db [$key] = $instance;
					self::register($instance, $regkey);
				} else {
					$classname = 'Db_' . ucfirst(strtolower($engine));
					if (self::loadClass ( 'db/' . $engine ) && class_exists ( $classname )) {
						$instance = new $classname( $config );
						if ($instance)
							//self::$db [$key] = $instance;
							self::register($instance, $regkey);
						else
							exit('MidPHP Error: DB config or class is not be found!');
					}
				}
			}
		}else{
			$instance = self::registry($regkey);
		}
		//return self::$db [$key];
		return $instance;
	}
	
	/**
	 * statis smarty object storage
	 *
	 * @return smarty object
	 */
	public function smarty() {
		$regkey = 'smarty';
		if (!self::isRegistered($regkey)) {
			if (self::loadLib ( 'smarty/Smarty.class' ) && class_exists ( 'Smarty' )) {
				//self::$smarty = new Smarty ( );
				$instance = new Smarty();
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: cound found smarty class!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		//return self::$smarty;
		return $instance;
	}
	
	/**
	 * Quick get config
	 *
	 * @param config name
	 * @return array or string or object
	 */
	public static function config($id) {
		/*static $loaded = NULL;
		if($loaded===NULL){
			self::loadClass('config');
			$loaded = true;
		}*/
		return Config::Get ( $id );
	}
	
	/**
	 * use cache
	 *
	 * @param string $type (apc,file,xcache,memory,memcache),default:file
	 * @return object cache
	 */
	public static function cache($type = 'file') {
		$regkey = 'cache.' . $type;
		if (!self::isRegistered($regkey)) {
			if (self::loadClass ( 'cache' ) && class_exists ( 'cache' )) {
				self::loadClass ( "cache" );
				//self::$cache [$type] = Cache::instance ( $type );
				$instance = Cache::instance ( $type );
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: cound not found cache class!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		//return self::$cache [$type];
		return $instance;
	}
	
	/**
	 * application object storage
	 *
	 * @return object application
	 */
	public function app() {
		$regkey = 'app';
		if ( !self::isRegistered($regkey)) {
			if (self::loadClass ( 'application' ) && class_exists ( 'Application' )) {
				//self::$app = new Application ( );
				$instance = new Application();
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: Application class is not exist!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		//return self::$app;
		return $instance;
	}
	
	/**
	 * application object storage
	 *
	 * @return object application
	 */
	public function dbmagic( $name = 'default') {
		$regkey = 'dbmagic.' . $name;
		if ( !self::isRegistered($regkey)) {
			if (self::loadClass ( 'db/dbmagic' ) && class_exists ( 'DBMagic' )) {
				$instance = new DBMagic( $name );
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: DBMagic class is not exist!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		return $instance;
	}
	
	/**
	 * template object storage
	 *
	 * @return object application
	 */
	public function template() {
		$regkey = 'template';
		if ( !self::isRegistered($regkey)) {
			if (self::loadClass ( 'template' ) && class_exists ( 'Template' )) {
				$instance = Template::Instance();
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: Template class is not exist!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		return $instance;
	}
	
	/**
	 * application object storage
	 *
	 * @return object application
	 */
	public function module($name, $path = NULL) {
		if (!$name)
			exit ( 'Module Error: cound not found module name!' );
		$regkey = 'module.'. $name;
		if ( !self::isRegistered($regkey)) {
			$classname = ucfirst ( 'Module_' ) . ucfirst ( $name );
			if (NULL === $path) {
				$path = $name;
			}
			if (self::loadModule ( $path ) && class_exists ( $classname )) {
				$instance = new $classname ( );
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: Module class ' . $classname . ' is not exist!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		//return self::$module [$name];
		return $instance;
	}
	
	/**
	 * application object storage
	 *
	 * @return object application
	 */
	public function helper($name, $path = NULL) {
		$regkey = 'helper.' . $name;
		if (! self::isRegistered($regkey)) {
			$classname = ucfirst ( 'Helper_' ) . ucfirst ( $name );
			if (NULL === $path) {
				$path = $name;
			}
			if (self::loadHelper ( $path ) && class_exists ( $classname )) {
				//self::$helper [$name] = new $classname ( );
				$instance = new $classname ( );
				self::register($instance, $regkey);
			} else {
				exit ( 'MidPHP Error: Helper class ' . $classname . ' is not exist!' );
			}
		}else{
			$instance = self::registry($regkey);
		}
		//return self::$helper [$name];
		return $instance;
	}
	
	/**
	 * Load any file from your project and core
	 *
	 * @param string path like:"$:class:midp" or "m:module:index"
	 * @return include file
	 */
	public function load($str) {
		static $imported_files = array ();
		if (! empty ( $imported_files [$str] ) && $imported_files [$str] === 1) {
			return TRUE;
		}
		$path = self::path ( $str );
		if (! file_exists ( $path )) {
			exit ( "Load Error: $path is not exist!" );
		}
		if (DEBUG === TRUE) {
			echo '--> Load File:' . $path . '<br/>';
		}
		$imported_files [$str] = 1;
		return include_once ($path);
	}
	
	/**
	 * Get real path.
	 *
	 * @param string $str
	 * @return string
	 */
	function path($str) {
		$path = $str;
		if (strpos ( $path, '$' ) !== false) {
			$path = str_replace ( '$/', '', $path );
			$path = CORE_ROOT . str_replace ( '/', DIR_SEP, $path );
		} else {
			$path = APP_ROOT . str_replace ( '/', DIR_SEP, $path );
		}
		$path = $path . '.php';
		return $path;
	}
	
	/**
	 * Load file from core
	 *
	 * @param string like:"class:smarty"
	 * @return include file
	 */
	public function loadCore($str) {
		return self::load ( '$/' . $str );
	}
	
	/**
	 * Load file from core
	 *
	 * @param string like:"class:smarty"
	 * @return include file
	 */
	public function loadFunc($str) {
		return self::load ( '$/function/' . $str );
	}
	
	/**
	 * Load file from Class floder
	 *
	 * @param string like:"smarty"
	 * @return include file
	 */
	public function loadClass($str) {
		return self::loadCore ( $str );
	}
	
	/**
	 * Load file from library floder "lib"
	 *
	 * @param string like:"smarty"
	 * @return include file
	 */
	public function loadLib($str) {
		return self::load ( '$/library/' . $str );
	}
	
	/**
	 * Load file from library floder "lib"
	 *
	 * @param string like:"smarty"
	 * @return include file
	 */
	public function loadHelper($str) {
		return self::load ( '$/helper/' . $str );
	}
	
	/**
	 * Load file from library floder "lib"
	 *
	 * @param string like:"smarty"
	 * @return include file
	 */
	public function loadModule($str) {
		return self::load ( '$/module/' . $str );
	}
	
	/**
	 * Autoload file before application start
	 *
	 * @param array like:"array('$:class:smarty')"
	 * @return include file
	 */
	public function autoload($arr = array()) {
		if (is_array ( $arr ) && ! empty ( $arr )) {
			foreach ( $arr as $key => $load ) {
				foreach ( $load as $str ) {
					self::load ( $key . '/' . $str );
				}
			}
		}
		self::$autoload = $arr;
	}
	
	/**
	 * 验证码验证和其他验证,验证后将清空
	 *   适合验证通过session存储的任何值
	 *
	 * @param string $code
	 * @return string
	 */
	function verify($code, $name = 'securimage_code_value') {
		$value = $_SESSION [$name];
		if (isset ( $value ) && ! empty ( $value )) {
			if ($value == strtolower ( trim ( $code ) )) {
				$_SESSION [$name] = '';
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * get an object instance
	 *
	 * @param string like:"cache"
	 * @return object
	 */
	/*public function getHelper($path) {
		static $instance = array ();
		if (! isset ( $instance [$path] ) || ! $instance [$path]) {
			$instance [$path] = self::newObject ( $path, $classname );
		}
		return $instance [$path];
	}*/
	
	/**
	 * get an object instance
	 *
	 * @param string like:"cache"
	 * @return object
	 */
	/*public function getObject($path, $classname) {
		static $instance = array ();
		if (! isset ( $instance [$path] ) || ! $instance [$path]) {
			$instance [$path] = self::newObject ( $path, $classname );
		}
		return $instance [$path];
	}*/
	
	/**
	 * get an new object instance
	 *  只适合构造函数不需要参数的类
	 * @param string like:"cache"
	 * @return object
	 */
	/*public function newObject($path, $classname = null) {
		$classname = strtolower ( $path );
		self::load ( $path );
		if ($classname === null) {
			$sp_array = explode ( ':', $path );
			$classname = $sp_array [count ( $sp_array ) - 1];
		}
		if (class_exists ( $classname )) {
			$instance = new $classname ( );
		} else {
			exit ( 'new Object error:class ' . $classname . ' do not exist!' );
		}
		return $instance;
	}*/
	
	/**
	 * Register one object
	 *
	 * @param object $obj
	 * @param string $key
	 */
	public function register($obj, $key = NULL) {
		if (! is_object ( $obj )) {
			exit ( 'MidPHP Error: Only storage object!' );
		}
		if ($key === NULL) {
			$key = get_class ( $obj );
		}
		self::$poll_obj [$key] = $obj;
	}
	
	/**
	 * If the object has been registered
	 *
	 * @param string $key
	 * @return bool
	 */
	public function isRegistered($key) {
		if (isset ( self::$pool_obj [$key] )) {
			return true;
		}
		return false;
	}
	
	/**
	 * Get registered obj
	 *
	 * @param string $key
	 * @return object
	 */
	public function registry($key) {
		if (isset ( self::$pool_obj [$key] )) {
			return self::$pool_obj [$key];
		}
		return false;
		//exit ( 'MidPHP Error: Could not found reigstered object!' );
	}
	
	/**
	 * return midp version
	 *
	 * @return string
	 */
	public function version() {
		return '0.1.090722';
	}
	
	/**
	 * return midp version
	 *
	 * @return string
	 */
	public function Runtime() {
		$debuger = self::helper ( 'debuger' );
		return $debuger->getRuntime ();
		/*global $time_start;
		list ( $usec, $sec ) = explode ( " ", microtime () );
		return (( float ) $usec + ( float ) $sec) - $time_start;*/
	}

}
?>